// $ANTLR 3.3 Nov 30, 2010 12:50:56 /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g 2012-03-26 13:17:41

  import java.util.HashMap;
  import java.util.Vector;


import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

public class LispyParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "NUM", "ID", "WS", "'('", "')'", "'read'", "'print'", "'let'", "'repeat'"
    };
    public static final int EOF=-1;
    public static final int T__7=7;
    public static final int T__8=8;
    public static final int T__9=9;
    public static final int T__10=10;
    public static final int T__11=11;
    public static final int T__12=12;
    public static final int NUM=4;
    public static final int ID=5;
    public static final int WS=6;

    // delegates
    // delegators


        public LispyParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public LispyParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return LispyParser.tokenNames; }
    public String getGrammarFileName() { return "/home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g"; }


      HashMap<String, Expr> syms = new HashMap<String, Expr>();



    // $ANTLR start "prog"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:12:1: prog : ( expr )+ EOF ;
    public final void prog() throws RecognitionException {
        Expr expr1 = null;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:13:3: ( ( expr )+ EOF )
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:13:5: ( expr )+ EOF
            {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:13:5: ( expr )+
            int cnt1=0;
            loop1:
            do {
                int alt1=2;
                int LA1_0 = input.LA(1);

                if ( ((LA1_0>=NUM && LA1_0<=ID)||LA1_0==7) ) {
                    alt1=1;
                }


                switch (alt1) {
            	case 1 :
            	    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:13:6: expr
            	    {
            	    pushFollow(FOLLOW_expr_in_prog25);
            	    expr1=expr();

            	    state._fsp--;


            	        int i = expr1.run();
            	        System.out.println(">>" + i);
            	      

            	    }
            	    break;

            	default :
            	    if ( cnt1 >= 1 ) break loop1;
                        EarlyExitException eee =
                            new EarlyExitException(1, input);
                        throw eee;
                }
                cnt1++;
            } while (true);

            match(input,EOF,FOLLOW_EOF_in_prog31); 

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "prog"


    // $ANTLR start "expr"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:19:1: expr returns [Expr code] : ( NUM | ID | '(' fun (e= expr )* ')' );
    public final Expr expr() throws RecognitionException {
        Expr code = null;

        Token NUM2=null;
        Token ID3=null;
        Expr e = null;

        Fun fun4 = null;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:20:3: ( NUM | ID | '(' fun (e= expr )* ')' )
            int alt3=3;
            switch ( input.LA(1) ) {
            case NUM:
                {
                alt3=1;
                }
                break;
            case ID:
                {
                alt3=2;
                }
                break;
            case 7:
                {
                alt3=3;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 3, 0, input);

                throw nvae;
            }

            switch (alt3) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:20:5: NUM
                    {
                    NUM2=(Token)match(input,NUM,FOLLOW_NUM_in_expr50); 

                          code = new NumExpr(syms, (NUM2!=null?NUM2.getText():null));
                        

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:23:5: ID
                    {
                    ID3=(Token)match(input,ID,FOLLOW_ID_in_expr58); 

                        code = new SymExpr(syms, (ID3!=null?ID3.getText():null));
                      

                    }
                    break;
                case 3 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:26:5: '(' fun (e= expr )* ')'
                    {
                    match(input,7,FOLLOW_7_in_expr66); 
                    Vector args = new Vector();
                    pushFollow(FOLLOW_fun_in_expr78);
                    fun4=fun();

                    state._fsp--;

                    Fun f = fun4;
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:29:5: (e= expr )*
                    loop2:
                    do {
                        int alt2=2;
                        int LA2_0 = input.LA(1);

                        if ( ((LA2_0>=NUM && LA2_0<=ID)||LA2_0==7) ) {
                            alt2=1;
                        }


                        switch (alt2) {
                    	case 1 :
                    	    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:29:6: e= expr
                    	    {
                    	    pushFollow(FOLLOW_expr_in_expr89);
                    	    e=expr();

                    	    state._fsp--;

                    	    args.add(e);

                    	    }
                    	    break;

                    	default :
                    	    break loop2;
                        }
                    } while (true);

                    match(input,8,FOLLOW_8_in_expr100); 

                          try {
                            code = new FunExpr(syms, f, args);
                          } catch(Exception exception) {
                            System.err.println("ERROR: " + exception);
                            exception.printStackTrace();
                            code = null;
                          }
                        

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return code;
    }
    // $ANTLR end "expr"


    // $ANTLR start "fun"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:42:1: fun returns [Fun f] : ( 'read' | 'print' | 'let' ID | 'repeat' ID );
    public final Fun fun() throws RecognitionException {
        Fun f = null;

        Token ID5=null;
        Token ID6=null;

        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:43:3: ( 'read' | 'print' | 'let' ID | 'repeat' ID )
            int alt4=4;
            switch ( input.LA(1) ) {
            case 9:
                {
                alt4=1;
                }
                break;
            case 10:
                {
                alt4=2;
                }
                break;
            case 11:
                {
                alt4=3;
                }
                break;
            case 12:
                {
                alt4=4;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 4, 0, input);

                throw nvae;
            }

            switch (alt4) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:43:5: 'read'
                    {
                    match(input,9,FOLLOW_9_in_fun123); 
                    f = new ReadFun(syms);

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:44:5: 'print'
                    {
                    match(input,10,FOLLOW_10_in_fun134); 
                    f = new PrintFun(syms);

                    }
                    break;
                case 3 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:45:5: 'let' ID
                    {
                    match(input,11,FOLLOW_11_in_fun144); 
                    ID5=(Token)match(input,ID,FOLLOW_ID_in_fun146); 
                    f = new LetFun(syms, (ID5!=null?ID5.getText():null));

                    }
                    break;
                case 4 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/06-lispy-language/src/Lispy.g:46:5: 'repeat' ID
                    {
                    match(input,12,FOLLOW_12_in_fun155); 
                    ID6=(Token)match(input,ID,FOLLOW_ID_in_fun157); 
                    f = new ForLoopFun(syms, (ID6!=null?ID6.getText():null));

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return f;
    }
    // $ANTLR end "fun"

    // Delegated rules


 

    public static final BitSet FOLLOW_expr_in_prog25 = new BitSet(new long[]{0x00000000000000B0L});
    public static final BitSet FOLLOW_EOF_in_prog31 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_NUM_in_expr50 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_ID_in_expr58 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_7_in_expr66 = new BitSet(new long[]{0x0000000000001E00L});
    public static final BitSet FOLLOW_fun_in_expr78 = new BitSet(new long[]{0x00000000000001B0L});
    public static final BitSet FOLLOW_expr_in_expr89 = new BitSet(new long[]{0x00000000000001B0L});
    public static final BitSet FOLLOW_8_in_expr100 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_9_in_fun123 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_10_in_fun134 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_11_in_fun144 = new BitSet(new long[]{0x0000000000000020L});
    public static final BitSet FOLLOW_ID_in_fun146 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_12_in_fun155 = new BitSet(new long[]{0x0000000000000020L});
    public static final BitSet FOLLOW_ID_in_fun157 = new BitSet(new long[]{0x0000000000000002L});

}